package com.jackeysun.app_common

import android.content.Context
import android.content.res.AssetManager
import android.graphics.Color
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.LayoutInflaterCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jackeysun.app_common.databinding.ActivityMain2Binding
import com.jackeysun.app_common.databinding.ItemView0Binding
import com.jackeysun.app_common.databinding.ItemView1Binding
import com.jackeysun.common.*
import com.jackeysun.util.toJson
import com.jackeysun.util.toObject

class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val main2Binding = ActivityMain2Binding.inflate(layoutInflater)
        setContentView(main2Binding.root)

        val adapter = TestAdapter()
//        val adapter = AbsAdapter<TestData>()
//        onCreate2(R.layout.item_view0.to(1, R.layout.item_view1))
        adapter.addLayout(0, R.layout.item_view0/*.to(1, R.layout.item_view1)*/)
        adapter.addClickId(R.id.tv)
//        adapter.addClickId(R.id.tv2)

        var datas = mutableListOf<TestData>()
        for (i in 0..100) {
            datas.add(TestData("第${i}项"))
        }
        adapter.data = datas
        main2Binding.rv.adapter = adapter
        main2Binding.rv.layoutManager = LinearLayoutManager(this)

        main2Binding.btn.setOnClickListener {
            Log.d("TAG", "onCreate: ${datas}")
            val datas2 = adapter.data.toJson().toObject<MutableList<TestData>>()
            Log.d("TAG", "onCreate: ${datas2}")
            datas2.forEachIndexed { index, testData ->
                if (index == 3) {
                    testData.background = if (testData.background == Color.GRAY) {
                        Color.RED
                    } else {
                        Color.GRAY
                    }
                }
            }
            adapter.notifyDataChanged(datas2) { oldItemPosition, newItemPosition ->
                val oldBackground = adapter.data[oldItemPosition].background
                val newBackground = datas2[newItemPosition].background
                if (oldBackground != newBackground) {
                    return@notifyDataChanged "background"
                }
                return@notifyDataChanged null
            }
//            val diffResult: DiffUtil.DiffResult =
//                DiffUtil.calculateDiff(ListCallback(datas, datas2))
//            diffResult.dispatchUpdatesTo(adapter)
//            adapter.data = datas2
//            datas.clear()
//            datas = datas2
        }
    }

    private fun onCreate2(to: Int) {
        LayoutInflaterCompat.setFactory2(layoutInflater, object : LayoutInflater.Factory2 {
            override fun onCreateView(
                parent: View?,
                name: String,
                context: Context,
                attrs: AttributeSet,
            ): View? {
                return null
            }

            override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {

                return null
            }

        })
    }


}

class TestAdapter : AbsAdapter<TestData>() {
    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int,
        payloads: MutableList<Any>,
    ) {
        if (payloads.isNullOrEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            val mBinding = ItemView0Binding.bind(holder.itemView)
            when (payloads[0] as String) {
                "background" -> mBinding.tv.setBackgroundColor(data[position].background)
            }
        }

    }
}

data class TestData(var content: String, var background: Int = Color.RED) : IData {

//    override fun getType(): Int {
//        return type
//    }

    override fun bind(holder: RecyclerView.ViewHolder) {
//        when (type) {
//            0 -> {
        val mBinding = ItemView0Binding.bind(holder.itemView)
        mBinding.tv.text = content
        mBinding.tv.setBackgroundColor(background)
    }
//            1 -> {
//                val mBinding = ItemView1Binding.bind(holder.itemView)
//                mBinding.tv2.text = content
//            }
//        }
//    }
}

class ListCallback(val oldList: List<TestData>, val newList: List<TestData>) : DiffUtil.Callback() {
    override fun getOldListSize(): Int = oldList.size

    override fun getNewListSize(): Int = newList.size

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return true
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition].background == newList[newItemPosition].background
    }

    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
//        return super.getChangePayload(oldItemPosition, newItemPosition)
        val oldBackground = oldList[oldItemPosition].background
        val newBackground = newList[oldItemPosition].background
        if (oldBackground != newBackground) {
            val bundle = Bundle()
            bundle.putInt("background", newBackground)
            return bundle
        }
        return null
    }

}


@LayoutRes
fun Int.to(theme: Int, @LayoutRes id: Int): Int =
    when (theme) {
        0 -> this
        1 -> id
        else -> this
    }








